﻿using System;
using System.Collections.Generic;
using System.Windows.Media;
using Might.Infrastructure.Annotations.Graphics;
using Might.Infrastructure.DOM;
using Might.Infrastructure.Events;
using Might.Infrastructure.ModelicaDOM;
using Might.Infrastructure.Services;

namespace Might.Modelica.Annotations.Graphics {
    ///<summary>
    ///</summary>
    [Mapping("GraphicItem")]
    public abstract class GraphicsItem : DomNode, IGraphicsItem {
        /// <summary>
        /// Public constructor for generic GraphicsPrimtive type.
        /// </summary>
        protected GraphicsItem() {
            Visible = true;
            origin = new GraphicsPoint(0,0);
            rotation = 0;
        }

        #region IGraphicsItem Members

        [Mapping("visible")]
        public bool Visible {
            get { return visible; }
            set {
                if (visible != value) {
                    visible = value;
                    OnChanged(this, "Visible");
                }
            }
        }
        private bool visible;

        public virtual double Width { get; set; }
        public virtual double Height { get; set; }
        public virtual double Top { get; set; }
        public virtual double Left { get; set; }

        public Matrix Transform { get; set; }
        
        public void Select() {
            CoreServices.EventAggregator.GetEvent<GraphicsPrimitiveSelectedEvent>().Publish(this);
        }

        #endregion

        #region Specific Modelica members

        ///<summary>
        ///</summary>
        [Mapping("origin")]
        public GraphicsPoint origin { get; set; }

        ///<summary>
        ///</summary>
        [Mapping("rotation")]
        public double rotation { get; set; }

        [UnknownMapping]
        public List<object> UnknownItems { get; set; }

        /// <summary>
        /// </summary>
        protected GraphicsExtent ParentExtent {
            get {
                Icon i = Parent as Icon;
                if (i == null) {
                    throw new NullReferenceException("Parent is null!");
                }
                return i.coordinateSystem.Extent;
            }
        }

        public virtual bool IsDefault {
            get {
                return visible &&
                    origin.IsDefault &&
                    rotation == 0;
            }
        }

        #endregion
    }
}